<%--
  Created by IntelliJ IDEA.
  User: lenovo
  Date: 2022/3/13
  Time: 13:54
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="/common/common.jsp" %>

<style>
    .layui-form-item .layui-input-company {
        width: auto;
        padding-right: 10px;
        line-height: 38px;
    }
    #updatePassword{
        margin: auto;
        padding-top: 90px;
        width: 600px;
        height: 500px;
    }
</style>
<div id="updatePassword" class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form">

            <div class="layui-form-item" style="padding-top: 50px">
                <label class="layui-form-label required">用户名</label>
                <div class="layui-input-block">
                    <input type="text" readonly="readonly" name="new_password" lay-verify="required"
                            value="${USER.username}" class="layui-input">
                    <div style="color: red;font-size: 6px;">修改用户名请找超级管理员</div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">旧的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="old_password" lay-verify="required|oldpasswod" lay-reqtext="旧的密码不能为空"
                           placeholder="请输入旧的密码" value="" class="layui-input">
                    <input type="hidden" id="old_password" value="${USER.password}">
                    <tip>填写自己账号的旧的密码。</tip>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">新的密码</label>
                <div class="layui-input-block">
                    <input type="password" id="new_password" name="new_password" lay-verify="required" lay-reqtext="新的密码不能为空"
                           placeholder="请输入新的密码" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label required">再次输入新的密码</label>
                <div class="layui-input-block">
                    <input type="password" name="again_password" lay-verify="required|confirmPass"
                           lay-reqtext="新的密码不能为空"
                           placeholder="请输入新的密码" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
                </div>
            </div>
        </div>
    </div>
</div>

<script>

    layui.use(['form', 'miniPage'], function () {
        var form = layui.form,
            layer = layui.layer,
            miniPage = layui.miniPage,
            $ = layui.$;

        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var info = data.field;
            console.info(info)
            $.get("updatePassword.do", {password: info.again_password, id:${USER.id}}, function (data) {
                if (data.success) {
                    layer.msg('修改成功，请重新登录', function () {
                        window.location.href = "toLogout.do";
                    });
                } else {
                    layer.msg(data.error);
                }
            }, "json")

            return false;
        });
        form.verify({
            oldpasswod: function (value, item) { //value：表单的值、item：表单的DOM对象
                //id为old_password的值和密码输入框校验
                if ($('input[id=old_password]').val() !== value)
                    return '旧密码输入错误';

            },
            confirmPass: function (value, item) { //value：表单的值、item：表单的DOM对象

                if ($('input[id=new_password]').val() !== value)
                    return '两次密码输入不一致！';


                if ($('input[id=old_password]').val() == value)
                    return '新密码和旧密码不能一样';




                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '密码不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '密码首尾不能出现下划线\'_\'';
                }
            }
        });

    });
</script>
